<?php

namespace App\Console\Commands\Cruise;

use App\Models\Common\ConfigModel;
use App\Models\Cruise\CruiseCheckAlbumModel;
use App\Models\Cruise\CruiseShopLicenseRecordsLogModel;
use App\Models\Cruise\CruiseShopLicenseRecordsModel;
use App\Models\Cruise\CruiseShopLicenseResourceRecordsModel;
use App\Remote\Rms\RmsUserRemote;
use App\Service\System\SystemNotifyService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Console\Command;

class CruiseCheckLicenceExpired extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cruise:check_licence_expired';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '稽核脚本查询过期证件照';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @throws GuzzleException
     */
    public function handle()
    {
        CruiseShopLicenseRecordsModel::query()
//                                     ->where('shopId', 18)
//                                     ->whereNotIn('status', [
//                CruiseShopLicenseRecordsModel::STATUS_PENDING,
//            ])
                                     ->chunkById(200, function ($item) {
                foreach ($item as $one) {
                    $list = CruiseShopLicenseResourceRecordsModel::query()
                                                                 ->where('shopId',
                                                                     $one->shopId)
                                                                 ->get();

                    $logInsert = [];

                    foreach (
                        [
                            CruiseShopLicenseResourceRecordsModel::TYPE_BUSINESS_LICENSE,
                            CruiseShopLicenseResourceRecordsModel::TYPE_FOOD_LICENSE
                        ] as $type
                    ) {
                        $license = $list->where('type', $type)->first();

                        if (empty($license)) {
                            $licenseStatus[$type] = CruiseShopLicenseRecordsModel::STATUS_LACK;
                            continue;
                        }

                        $licenseStatus[$type] = $license->status;

                        if ($license->licenseEndDate < time()) {
                            // 过期了
                            $licenseStatus[$type] = CruiseShopLicenseRecordsModel::STATUS_EXPIRED;

                            $logInsert[] = [
                                'shopId'      => $one->shopId,
                                'operateType' => CruiseShopLicenseRecordsLogModel::OPERATE_TYPE_SYS,
                                'actionType'  => CruiseShopLicenseRecordsLogModel::ACTION_TYPE_CARD_EXPIRED,
                                'type'        => $type,
                                'createdAt'   => time(),
                                'updatedAt'   => time(),
                            ];
                            $license->status = CruiseShopLicenseRecordsModel::STATUS_EXPIRED;
                            $license->save();
                        }
                    }

                    $mainStatus = CruiseShopLicenseRecordsModel::getStatus(
                        $licenseStatus[CruiseShopLicenseResourceRecordsModel::TYPE_BUSINESS_LICENSE],
                        $licenseStatus[CruiseShopLicenseResourceRecordsModel::TYPE_FOOD_LICENSE]
                    );


//                    if ($one->status != $mainStatus) {
                    $one->status = $mainStatus;
                    $one->save();

                    CruiseShopLicenseRecordsLogModel::query()->insert($logInsert);
//                    }
                }
            });

    }
}
